import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/date_picker/custom_date_picker_sheet.dart';
import 'package:flutter_date_pickers/flutter_date_pickers.dart';
import 'package:get/get.dart';

class UtilsDeposit {
  /// 将DateTime转换为API所需的格式 YYYY-MM-DD+HH:mm:ss
  static String formatDateTimeForApi(DateTime dateTime) {
    return '${dateTime.year.toString().padLeft(4, '0')}-'
        '${dateTime.month.toString().padLeft(2, '0')}-'
        '${dateTime.day.toString().padLeft(2, '0')} '
        '${dateTime.hour.toString().padLeft(2, '0')}:'
        '${dateTime.minute.toString().padLeft(2, '0')}:'
        '${dateTime.second.toString().padLeft(2, '0')}';
  }

  // Rx<DatePeriod?> selectedDateRange
  /// 根据选择的时间类型计算日期范围
  static Map<String, String> calculateDateRange(String timeStr,
      {DatePeriod? selectedDateRange}) {
    final now = DateTime.now();
    DateTime startDate;
    DateTime endDate;

    if (timeStr == LocaleKeys.today.tr) {
      // 当天：00:00:00 到 23:59:59
      startDate = DateTime(now.year, now.month, now.day, 0, 0, 0);
      endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    } else if (timeStr == LocaleKeys.yesterday.tr) {
      // 昨天：昨天的00:00:00 到 23:59:59
      final yesterday = now.subtract(const Duration(days: 1));
      startDate =
          DateTime(yesterday.year, yesterday.month, yesterday.day, 0, 0, 0);
      endDate =
          DateTime(yesterday.year, yesterday.month, yesterday.day, 23, 59, 59);
    } else if (timeStr == LocaleKeys.threeDays.tr) {
      // 三天：三天前00:00:00 到今天23:59:59
      final threeDaysAgo = now.subtract(const Duration(days: 2));
      startDate = DateTime(
          threeDaysAgo.year, threeDaysAgo.month, threeDaysAgo.day, 0, 0, 0);
      endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    } else if (timeStr == LocaleKeys.sevenDays.tr) {
      // 七天：七天前00:00:00 到今天23:59:59
      final sevenDaysAgo = now.subtract(const Duration(days: 6));
      startDate = DateTime(
          sevenDaysAgo.year, sevenDaysAgo.month, sevenDaysAgo.day, 0, 0, 0);
      endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    } else if (timeStr == LocaleKeys.custom.tr && selectedDateRange != null) {
      // 自定义：用户选择的范围
      startDate = DateTime(selectedDateRange.start.year,
          selectedDateRange.start.month, selectedDateRange.start.day, 0, 0, 0);
      endDate = DateTime(selectedDateRange.end.year,
          selectedDateRange.end.month, selectedDateRange.end.day, 23, 59, 59);
    } else {
      // 默认为今天
      startDate = DateTime(now.year, now.month, now.day, 0, 0, 0);
      endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    }

    return {
      'start': formatDateTimeForApi(startDate),
      'end': formatDateTimeForApi(endDate),
    };
  }

  /// 显示日期范围选择器
  static  showDateRangePicker(DatePeriod? initialDateRange) async {
    final result = await CustomDatePickerSheet.show(
      initialDateRange: initialDateRange,
      firstDate: DateTime(2020),
      lastDate: DateTime.now(),
      isRangeSelection: true,
    );

    return result;
  }
}
